package com.jysz.certificate.persistence.model.cm;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.jysz.certificate.persistence.enums.CertificateRenewState;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;


/**
 * 合格证换证
 * 
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Entity
@Table(name="cm_certificate_renew_")
@org.hibernate.annotations.Entity
public class CertificateRenew extends AbstractBaseEntity<Long> {
	
	private static final long serialVersionUID = -2168956211139583647L;
	
	/** 金融机构*/
	private Financial financial;
	
	/** 店铺*/
	private Shop shop;
	
	/** 主机厂*/
	private Brand brand;
	
	// 申请单号
	private String applyNum;
	
	/** 申请人*/
	private User applyUser;
	
	/** 申请理由*/
	private String msg;
	
	/** 申请时间*/
	private Date applyDate;
	
	/** 换出时间*/          
	private Date renewDate;

	private double supplyPrice;// 供货价 
	private double inSupplyPrice;//换入供货价总数
	
	/** 状态*/
	private CertificateRenewState state;
	
	/** 换出的合格证*/
	private Set<Certification> outCertifications;
	
	private List<RenewObject> inCertificateRenews;

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_financial_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Financial getFinancial() {
		return financial;
	}

	public void setFinancial(Financial financial) {
		this.financial = financial;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_shop_id",columnDefinition="bigint(20)")
	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_brand_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	@Column(name= "_apply_num", nullable=false,columnDefinition="varchar(255)")
	public String getApplyNum() {
		return applyNum;
	}

	public void setApplyNum(String applyNum) {
		this.applyNum = applyNum;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_apply_user_id",columnDefinition="bigint(20)")
	public User getApplyUser() {
		return applyUser;
	}

	public void setApplyUser(User applyUser) {
		this.applyUser = applyUser;
	}

	@Column(name= "_apply_date",nullable=false)
	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	@Column(name= "_renew_date",nullable=true)
	public Date getRenewDate() {
		return renewDate;
	}

	public void setRenewDate(Date renewDate) {
		this.renewDate = renewDate;
	}

	@Column(name = "_state", nullable = false,columnDefinition="int(11)")
	@Enumerated(EnumType.ORDINAL)
	public CertificateRenewState getState() {
		return state;
	}

	public void setState(CertificateRenewState state) {
		this.state = state;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "rs_cm_certificate_renew_cm_certification_", joinColumns = @JoinColumn(name = "_certificate_renew_id"), inverseJoinColumns = @JoinColumn(name = "_certification_id"))
	public Set<Certification> getOutCertifications() {
		return outCertifications;
	}

	public void setOutCertifications(Set<Certification> outCertifications) {
		this.outCertifications = outCertifications;
	}
	
	@Column(name = "_supplyPrice",columnDefinition="numeric(9,2)")
	public double getSupplyPrice() {
		return supplyPrice;
	}
	public void setSupplyPrice(double supplyPrice) {
		this.supplyPrice = supplyPrice;
	}

	@OrderBy("createdAt desc")
	@OneToMany(targetEntity = RenewObject.class, mappedBy = "certificateRenew", cascade = { CascadeType.ALL })  
	public List<RenewObject> getInCertificateRenews() {
		return inCertificateRenews;
	}

	public void setInCertificateRenews(List<RenewObject> inCertificateRenews) {
		this.inCertificateRenews = inCertificateRenews;
	}

	@Column(name = "_in_supplyPrice",columnDefinition="numeric(9,2)")
	public double getInSupplyPrice() {
		return inSupplyPrice;
	}

	public void setInSupplyPrice(double inSupplyPrice) {
		this.inSupplyPrice = inSupplyPrice;
	}

	@Column(name = "_msg",columnDefinition="varchar(255)")
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}
